<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="活动主题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入活动主题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> 
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mall:activity:add']"
        >新增</el-button>
      </el-col> 
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="activityList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" type="index" width="55"  />
      <el-table-column label="活动主题" align="title" prop="title" />
      <el-table-column label="分类" align="center" prop="cateName" />
      <el-table-column label="图片" align="center" prop="pic">
        <template slot-scope="{row}">
          <el-image  style="height: 60px"
            :src="row.pic" :preview-src-list="[row.pic]">
          </el-image>
        </template>
      </el-table-column>  
      <el-table-column label="简介" align="center" prop="introduction" :show-overflow-tooltip="true"/>
      <el-table-column label="发布时间" align="center" prop="createTime" />
      <!-- <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="截止时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="志愿地址" align="center" prop="address" />
      <el-table-column label="联系电话" align="center" prop="phone" /> -->
      <!-- <el-table-column label="1.招募中， 2.已结束" align="center" prop="state" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-link v-if="scope.row.orderNum==100"
            type="success"
            @click="handleTop(scope.row, 1)"
          >置顶</el-link>
          <el-link v-if="scope.row.orderNum==1"
            type="danger"
            @click="handleTop(scope.row, 100)"
          >取消置顶</el-link>
          <el-button style="margin-left:10px"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mall:activity:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mall:activity:del']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改active对话框 -->
    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal="false" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="活动主题" prop="title">
          <el-input v-model="form.title" placeholder="请输入活动主题" />
        </el-form-item>
        <el-form-item label="分类" prop="cateId">
          <el-select v-model="form.cateId" placeholder="请选择分类" >
            <el-option v-for="item in cataList" :key="item.id"
              :label="item.name"
              :value="item.id"></el-option> 
          </el-select>
        </el-form-item> 
        <el-form-item label="图片" prop="pic">
          <ImageUpload v-model="form.pic" />
        </el-form-item>
        <el-form-item label="简介" prop="introduction">
          <el-input v-model="form.introduction" placeholder="请输入简介" type='textarea' rows="7"/>
        </el-form-item>
        <el-form-item label="详情" prop="content">
          <!-- <editor v-model="form.content" :min-height="192"/> -->
          <el-input v-model="form.content" placeholder="请输入详情" type='textarea' rows="8"/>
        </el-form-item> 
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listActivity, getActivity, delActivity, addActivity, updateActivity, exportActivity } from "@/api/system/activity";
import { getCate } from "@/api/mall/cate";
import Editor from '@/components/Editor';
import ImageUpload from '@/components/ImageUpload';

export default {
  name: "Activity",
  components: {
    Editor, ImageUpload
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // active表格数据
      activityList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        title: null,
        pic: null,
        content: null,
        startTime: null,
        endTime: null,
        cateId: null,
        address: null,
        phone: null,
        state: null,
      },
      cataList:[],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
         title: [{ required: true, message: "主题不能为空", trigger: "blur" }],
         pic: [{ required: true, message: "图片不能为空", trigger: "blur" }],
         cateId: [{ required: true, message: "分类不能为空", trigger: "blur" }],
         content: [{ required: true, message: "详情不能为空", trigger: "blur" }],
      }
    };
  },
  created() {
    this.getList();
    this.getCateList()
  },
  methods: {
    /** 查询active列表 */
    getList() {
      this.loading = true;
      listActivity(this.queryParams).then(response => {
        this.activityList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    getCateList(){
      getCate({ current: 1, size: 100,type:2}).then(res => {
        console.log(res)
        this.cataList = res.data;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        pic: null,
        content: null,
        startTime: null,
        endTime: null,
        cateId: null,
        address: null,
        phone: null,
        state: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    
    handleTop(row, num){
      row.orderNum = num;
      updateActivity(row).then(response => {
        this.msgSuccess("操作成功");
        this.getList();
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getActivity(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateActivity(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addActivity(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delActivity(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有active数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportActivity(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
